extends PanelContainer

@onready var popTree = %popupTree

#当ability选择的时候
signal ability_selected(ability_data:AbilityEntity)

#func _ready():
	#recontruct_tree(popTree)

func show_poppanel():
	recontruct_tree(popTree)
	show()

# 创建树
func recontruct_tree(tree:Tree):
	tree.clear()
	var root = tree.create_item()
	for ability_id in GlobalVariables.get_all_ability_keys():
		var ability_data = GlobalVariables.get_ability(ability_id)
		if ability_data.parent_id =='NULL':
			create_tree(root,ability_data)

#遍历整个树，创建树
func create_tree(node:TreeItem,ability_data:AbilityEntity):
	var child = node.create_child()
	child.set_text(0,ability_data.name)
	child.set_metadata(0,ability_data)
	child.set_tooltip_text(0,ability_data.description)
	if len(ability_data.children_id)==0:
		return
	else:
		for subchild_id in ability_data.children_id:
			var subchild_data = GlobalVariables.get_ability(subchild_id)
			create_tree(child,subchild_data)

func _on_select_btn_pressed():
	#获得当前选择的treeItem
	var selected_item:TreeItem = popTree.get_selected()
	if selected_item==null:
		return 
	# 显示出技能名称
	#var ability_name = selected_item.get_text(0)
	# 获得对应的ability_id
	var ability_data = selected_item.get_metadata(0)
	self.hide()
	ability_selected.emit(ability_data)

func _on_cancel_btn_pressed():
	self.hide()

# 双击某一项目时
func _on_popup_tree_item_activated():
	_on_select_btn_pressed()
